package com.ljx.funchat.chat.dao;

import com.ljx.funchat.chat.domain.entity.Redbag;
import com.ljx.funchat.chat.mapper.RedbagMapper;
import com.ljx.funchat.chat.service.IRedbagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 红包表 服务实现类
 * </p>
 *
 * @author LiJunXi
 * @since 2024-02-18
 */
@Service
public class RedbagDao extends ServiceImpl<RedbagMapper, Redbag> {

    public void updateMsgIdById(Long redbagId, Long msg) {
        lambdaUpdate().eq(Redbag::getId, redbagId)
                .set(Redbag::getMessageId, msg)
                .update();
    }

    public void updateNumberById(Long redbagId, Integer randomMoney) {
        lambdaUpdate().eq(Redbag::getId, redbagId)
                .setSql("final_number = final_number - 1")
                .setSql("final_money = final_money - " + randomMoney)
                .gt(Redbag::getFinalNumber, 0) // 乐观锁
                .gt(Redbag::getFinalMoney, 0)
                .update();
    }
}
